<template>
  <div class="productProcess">
    <!-- 如果页面刷新数据比较频繁，可以将loading、showFlag的相关代码删除，防止过于频繁的出现加载动画 -->

    <div class="success_info_body" v-show="showFlag">
      <!-- 参数名称、列数根据实际情况调整 -->
      <div class="table_body">
        <div class="table_th">
          <div
            :style="{ width: props.isRent ? '25%' : '20%' }"
            class="tr th_style"
            v-for="item in props.lables"
            :key="item"
          >
            {{ item }}
          </div>
        </div>

        <div class="table_main_body">
          <div
            v-if="props.isRent"
            class="table_inner_body"
            :style="{ top: tableTop + 'px' }"
          >
            <div
              class="table_tr"
              v-for="(item, index) in props.tableData"
              :key="index"
            >
              <div class="tr tr1">{{ item.id }}</div>
              <div class="tr tr1">{{ item.partyACorpId }}</div>
              <div class="tr tr1">{{ item.name }}</div>
              <div class="tr tr1">{{ item.area }}</div>
            </div>
          </div>
          <div
            v-else
            class="table_inner_body"
            :style="{ top: tableTop + 'px' }"
          >
            <div
              class="table_tr"
              v-for="(item, index) in props.tableData"
              :key="index"
            >
              <div class="tr tr2">{{ item.name }}</div>
              <div class="tr tr2">{{ item.zczj }}</div>
              <div class="tr tr2">{{ item.zcdz }}</div>
              <div class="tr tr2">{{ item.doubleLandTime }}</div>

              <div class="tr tr2">{{ item.enterpriseTypeOneName || "-" }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, computed } from "vue";
const props = defineProps({
  tableData: {
    type: Array,
    default: () => {
      return [];
    },
  },
  lables: {
    type: Array,
    default: () => {
      return [];
    },
  },
  isRent: {
    type: Boolean,
    default: false,
  },
});

const showFlag = ref(true);
const tableTimer = ref(null);
const tableTop = ref(0);
const tableList = ref([
  {
    process: 0.0,
    planNo: "BP2022060701",
    endDate: "2022-06-07",
    type: "砌块",
    startDate: "2022-06-07",
  },
  {
    process: 0.0,
    planNo: "BP2022060701",
    endDate: "2022-06-07",
    type: "砌块",
    startDate: "2022-06-07",
  },
  {
    process: 0.0,
    planNo: "BP2022060701",
    endDate: "2022-06-07",
    type: "砌块",
    startDate: "2022-06-07",
  },
  {
    process: 0.0,
    planNo: "BP2022060701",
    endDate: "2022-06-07",
    type: "砌块",
    startDate: "2022-06-07",
  },
  {
    process: 0.0,
    planNo: "BP2022060701",
    endDate: "2022-06-07",
    type: "砌块",
    startDate: "2022-06-07",
  },
  {
    process: 0.0,
    planNo: "WP2022061301",
    endDate: "2022-06-13",
    type: "墙板",
    startDate: "2022-06-13",
  },
]);

const tableListSize = ref(0);
const componentTimer = ref(null);

//需要根据情况设置的参数
const title = ref("排产进度");
const visibleSize = ref(2); //容器内可视最大完整行数
const lineHeight = ref(49); //每行的实际高度（包含margin-top/bottom,border等）
const componentTimerInterval = ref(3600000); //刷新数据的时间间隔
const tableTimerInterval = ref(100); //向上滚动 1px 所需要的时间，越小越快，推荐值 100

const bsGetProductProcess = () => {
  clearInterval(tableTimer.value);
  tableTop.value = 0;
  showFlag.value = true;
  tableActionFun();
};

const tableActionFun = () => {
  tableListSize.value = tableList.value.length;
  if (tableListSize.value > visibleSize.value) {
    tableList.value = tableList.value.concat(tableList.value);
    tableTimerFun();
  } else {
    //fillTableList();
  }
};

//当数据过少时，不触发自动轮播事件，并填充没有数据的行，参数根据实际情况修改即可
const fillTableList = () => {
  var addLength = visibleSize.value - tableListSize.value;
  for (var i = 0; i < addLength; i++) {
    tableList.value.push({
      planNo: "-",
      type: "-",
      startDate: "-",
      endDate: "-",
      process: "-",
    });
  }
};

const tableTimerFun = () => {
  var count = 0;
  tableTimer.value = setInterval(() => {
    if (count < (tableList.value.length / 2) * lineHeight.value) {
      tableTop.value -= 1;
      count++;
    } else {
      count = 0;
      tableTop.value = 0;
    }
  }, tableTimerInterval.value);
};

const componentTimerFun = () => {
  //页面自动刷新时间
  componentTimer.value = setInterval(() => {
    bsGetProductProcess();
  }, componentTimerInterval.value);
};

onMounted(() => {
  clearInterval(componentTimer.value);
  bsGetProductProcess();
  componentTimerFun();
});

onUnmounted(() => {
  clearInterval(componentTimer.value);
  clearInterval(tableTimer.value);
});
</script>

<style scoped>
.productProcess {
  width: 100%;
  height: 315px;
}
.loading_div {
  color: #eee;
  padding-top: 100px;
}
.title_div {
  width: 100%;
}
.table_body {
  width: 100%;
  height: 100%;
  margin-top: 15px;
}
.table_th {
  width: 100%;
  display: flex;
  height: 40px;
  line-height: 40px;
  background: #388588;
}
.tr {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  box-sizing: border-box;
  padding: 0 5px;
  text-align: center;
  font-size: 14px;
}
.tr1 {
  width: 25%;
}
.tr2 {
  width: 20%;
}
.tr3 {
  width: 15%;
}
.tr4 {
  flex: 1;
}
.th_style {
  font-size: 14px;
  font-weight: bold;
  color: rgba(255, 255, 255, 0.8);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  box-sizing: border-box;
  padding: 0 5px;
  text-align: center;
}
.table_main_body {
  width: 100%;
  height: 205px;
  overflow: hidden;
  position: relative;
}
.table_inner_body {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
}
.table_tr {
  display: flex;
  height: 40px;
  line-height: 40px;
  color: #eee;
  font-size: 15px;
  background: rgba(3, 145, 167, 0.1);
  border: 1px solid rgb(4, 114, 131);
  margin-top: 7px;
}
</style>
